/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.swing.filechooser;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import javax.swing.filechooser.FileFilter;
import ca.forklabs.baselib.swing.filechooser.AbstractFileFilter;
import ca.forklabs.baselib.swing.filechooser.CompositeFileFilter;
import ca.forklabs.baselib.swing.filechooser.DirectoryFileFilter;
import ca.forklabs.baselib.util.Algorithm;
import ca.forklabs.baselib.util.UnaryFunction;

/**
 * Class {@code ExtensionFileFilter} is a {@link FileFilter} that filters
 * according to provided file extensions. Directories are let through.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.swing.filechooser.ExtensionFileFilter">Daniel Léonard</a>
 * @version $Revision$
 */
public class ExtensionFileFilter extends AbstractFileFilter {

//---------------------------
// Class variables
//---------------------------

   /** The file filter for directories. */
   @SuppressWarnings("nls")
   private static final FileFilter DIRECTORY_FILTER = new DirectoryFileFilter("");


//---------------------------
// Instance variables
//---------------------------

   /** The delegate composite file filter. */
   @SuppressWarnings("nls")
   private CompositeFileFilter delegate = new CompositeFileFilter("");


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    * @param   description   the description.
    */
   public ExtensionFileFilter(String description) {
      this(description, new String[0]);
      }

   /**
    * Constructor.
    * @param   description   the description.
    * @param   extensions   the initial extensions.
    */
   public ExtensionFileFilter(String description, String... extensions) {
      this(description, Arrays.asList(extensions));
      }

   /**
    * Constructor.
    * @param   description   the description.
    * @param   extensions   the initial extensions.
    */
   public ExtensionFileFilter(String description, Iterable<String> extensions) {
      super(description);
      this.addDirectoryFilter();
      this.addExtensions(extensions);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Gets the delegate file filter.
    * @return   the delegate file filter.
    */
   protected CompositeFileFilter getDelegateFilter() {
      return this.delegate;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets a file filter that filters only on the end of the filename when
    * filtering. The filtering on the name is case-sensitive. You can override
    * this method to change the filtering behaviour.
    * @param   extension   the extension.
    * @return   the file filter.
    */
   protected FileFilter getExtensionFileFilter(String extension) {
      final String ext = extension.toLowerCase();
      FileFilter filter = new FileFilter() {
         @Override public boolean accept(File file) {
            String name = file.getName();
            name = name.toLowerCase();
            boolean evaluation = name.endsWith(ext);
            return evaluation;
            }
         @Override public String getDescription() {
            return ext;
            }
         };
      return filter;
      }

   /**
    * Adds the directory filter to the delegate composite filter.
    */
   protected void addDirectoryFilter() {
      CompositeFileFilter composite_filter = this.getDelegateFilter();
      composite_filter.addFilter(ExtensionFileFilter.DIRECTORY_FILTER);
      }

   /**
    * Adds an extension to the filter.
    * @param   extension   the file extension.
    */
   public void addExtension(String extension) {
      CompositeFileFilter composite_filter = this.getDelegateFilter();
      FileFilter filter = this.getExtensionFileFilter(extension);
      composite_filter.addFilter(filter);
      }

   /**
    * Adds multiple extension to the filter.
    * @param   extensions   the extensions.
    */
   public void addExtensions(Iterable<String> extensions) {
      Iterator<String> iterator = extensions.iterator();
      Algorithm.forEach(iterator, new UnaryFunction<Void, String>() {
         @Override
         public Void invoke(String extension) {
            ExtensionFileFilter filter = ExtensionFileFilter.this;
            filter.addExtension(extension);
            return null;
            }
         });
      }


//---------------------------
// Overridden method from ca.forklabs.baselib.swing.filechooser.CompositeFileFilter
//---------------------------

   /**
    * Accepts the file if its extension is within the list of extension.
    * @param   file   the file to test.
    * @return   {@code true} if the file is accepted, {@code false} otherwise.
    */
   @Override
   public boolean accept(File file) {
      CompositeFileFilter composite_filter = this.getDelegateFilter();
      boolean acceptance = composite_filter.accept(file);
       return acceptance;
       }

   }
